﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApTcNewObjectCreationFactory
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using EngineeringInternalExtension.DataTypes;
using System;
using System.Collections.ObjectModel;
using System.Resources;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

public class CApTcNewObjectCreationFactory
{
  private string m_objName;
  private string m_objDescription;
  private TxPlanningTypeMetaData m_metaData;

  internal bool CreateChildByType(
    ITxObject parentObj,
    string objName,
    string objDescription,
    TxPlanningTypeMetaData metaData)
  {
    this.m_objName = objName;
    this.m_objDescription = objDescription;
    this.m_metaData = metaData;
    bool childByType = false;
    try
    {
      ITxCompound itxCompound = parentObj as ITxCompound;
      ITxCompoundOperation compoundOperation = parentObj as ITxCompoundOperation;
      ITxComponent itxComponent = parentObj as ITxComponent;
      if (itxCompound != null)
      {
        TxCompoundResource compoundResource = itxCompound as TxCompoundResource;
        if (TxCompoundResource.op_Inequality(compoundResource, (ITxObject) null))
          childByType = this.CreateChildUnderTcObject((ITxObject) compoundResource);
      }
      else if (compoundOperation != null)
      {
        TxTcOperation operation = compoundOperation as TxTcOperation;
        TxTcProcess process = compoundOperation as TxTcProcess;
        TxTcActivity activity = compoundOperation as TxTcActivity;
        if (TxTcActivity.op_Inequality(activity, (ITxObject) null))
          childByType = this.CreateChildUnderTcActivity(activity);
        else if (TxTcProcess.op_Inequality(process, (ITxObject) null))
          childByType = this.CreateChildUnderTcProcess(process);
        else if (TxTcOperation.op_Inequality(operation, (ITxObject) null))
          childByType = this.CreateChildUnderTcOperation(operation);
      }
      else if (itxComponent != null)
        childByType = this.CreateChildUnderTcObject((ITxObject) itxComponent);
      else if (parentObj is ITxLogicalCollectionCreation logicalParent)
        childByType = this.CreateChildUnderOccurreneGroup(logicalParent);
    }
    catch (Exception ex)
    {
      int num = (int) MessageBox.Show(ex.Message, this.GetResourceManager().GetString("NEW_OBJECT_CMD_NAME"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
    return childByType;
  }

  private bool CreateChildUnderOccurreneGroup(ITxLogicalCollectionCreation logicalParent)
  {
    TxLogicalCollectionCreationData collectionCreationData = new TxLogicalCollectionCreationData();
    ((TxObjectCreationData) collectionCreationData).Name = this.m_objName;
    collectionCreationData.TypeName = this.m_metaData.TypeName;
    bool compoundAssembly = logicalParent.CanCreateCompoundAssembly(collectionCreationData);
    if (compoundAssembly)
      this.SelectObject((ITxObject) logicalParent.CreateCompoundAssembly(collectionCreationData));
    return compoundAssembly;
  }

  private bool CreateChildUnderTcActivity(TxTcActivity activity)
  {
    bool childUnderTcActivity = false;
    if (TxTcActivity.op_Inequality(activity, (ITxObject) null) && this.m_metaData.IsDerivedFromPlanningType("MEActivity"))
    {
      TxTcActivityCreationData activityCreationData = new TxTcActivityCreationData();
      ((TxOperationCreationData) activityCreationData).Description = this.m_objDescription;
      ((TxObjectCreationData) activityCreationData).Name = this.m_objName;
      activityCreationData.TypeName = this.m_metaData.TypeName;
      ((TxOperationCreationData) activityCreationData).Duration = 5.0;
      if (activity.CanCreateTcActivity(activityCreationData))
      {
        this.SelectObject((ITxObject) activity.CreateTcActivity(activityCreationData));
        childUnderTcActivity = true;
      }
    }
    return childUnderTcActivity;
  }

  private bool CreateChildUnderTcOperation(TxTcOperation operation)
  {
    bool underTcOperation = false;
    if (TxTcOperation.op_Inequality(operation, (ITxObject) null) && this.m_metaData.IsDerivedFromPlanningType("MEActivity"))
    {
      TxTcActivityCreationData activityCreationData = new TxTcActivityCreationData();
      ((TxOperationCreationData) activityCreationData).Description = this.m_objDescription;
      ((TxObjectCreationData) activityCreationData).Name = this.m_objName;
      activityCreationData.TypeName = this.m_metaData.TypeName;
      ((TxOperationCreationData) activityCreationData).Duration = 5.0;
      if (operation.CanCreateTcActivity(activityCreationData))
      {
        this.SelectObject((ITxObject) operation.CreateTcActivity(activityCreationData));
        underTcOperation = true;
      }
    }
    return underTcOperation;
  }

  private bool CreateChildUnderTcProcess(TxTcProcess process)
  {
    bool childUnderTcProcess = false;
    if (TxTcProcess.op_Inequality(process, (ITxObject) null))
    {
      if (this.m_metaData.IsDerivedFromPlanningType("MEProcessRevision") || this.m_metaData.IsDerivedFromPlanningType("Mpr0ProcessElement"))
      {
        TxTcProcessCreationData processCreationData = new TxTcProcessCreationData();
        ((TxOperationCreationData) processCreationData).Description = this.m_objDescription;
        ((TxObjectCreationData) processCreationData).Name = this.m_objName;
        processCreationData.TypeName = this.m_metaData.TypeName;
        ((TxOperationCreationData) processCreationData).Duration = 0.0;
        if (process.CanCreateTcProcess(processCreationData))
        {
          this.SelectObject((ITxObject) process.CreateTcProcess(processCreationData));
          childUnderTcProcess = true;
        }
      }
      else if (this.m_metaData.IsDerivedFromPlanningType("MEOPRevision") || this.m_metaData.IsDerivedFromPlanningType("Mpr0Operation"))
      {
        if (this.m_metaData.IsDerivedFromPlanningType("Mfg0MEDiscreteOPRevision"))
          this.CreateTcDiscreteOperation(process);
        else if (this.m_metaData.IsDerivedFromPlanningType("Mfg0MEContJoinOPRevision"))
        {
          this.CreateTcContinuousOperation(process);
        }
        else
        {
          TxTcOperationCreationData operationCreationData = new TxTcOperationCreationData();
          ((TxOperationCreationData) operationCreationData).Description = this.m_objDescription;
          ((TxObjectCreationData) operationCreationData).Name = this.m_objName;
          operationCreationData.TypeName = this.m_metaData.TypeName;
          ((TxOperationCreationData) operationCreationData).Duration = 5.0;
          if (process.CanCreateTcOperation(operationCreationData))
            this.SelectObject((ITxObject) process.CreateTcOperation(operationCreationData));
        }
      }
    }
    return childUnderTcProcess;
  }

  private void CreateTcContinuousOperation(TxTcProcess process)
  {
    TxTcContinuousOperationCreationData operationCreationData = new TxTcContinuousOperationCreationData();
    ((TxOperationCreationData) operationCreationData).Description = this.m_objDescription;
    ((TxObjectCreationData) operationCreationData).Name = this.m_objName;
    operationCreationData.TypeName = this.m_metaData.TypeName;
    ((TxOperationCreationData) operationCreationData).Duration = 5.0;
    if (!process.CanCreateTcContinuousOperation(operationCreationData))
      return;
    this.SelectObject((ITxObject) process.CreateTcContinuousOperation(operationCreationData));
  }

  private void CreateTcDiscreteOperation(TxTcProcess process)
  {
    using (TxTcDiscreteOperationCreationData operationCreationData = new TxTcDiscreteOperationCreationData())
    {
      ((TxOperationCreationData) operationCreationData).Description = this.m_objDescription;
      ((TxObjectCreationData) operationCreationData).Name = this.m_objName;
      operationCreationData.TypeName = this.m_metaData.TypeName;
      ((TxOperationCreationData) operationCreationData).Duration = 5.0;
      if (!process.CanCreateTcDiscreteOperation(operationCreationData))
        return;
      this.SelectObject((ITxObject) process.CreateTcDiscreteOperation(operationCreationData));
    }
  }

  private bool CreateChildUnderTcObject(ITxObject obj)
  {
    bool childUnderTcObject = false;
    switch (obj)
    {
      case TxCompoundResource _:
label_2:
        using (TxTcResourceCreationDataEx resourceCreationDataEx = new TxTcResourceCreationDataEx(this.m_objName, this.m_metaData.TypeName))
        {
          if (TxTcUtilities.CanCreateTcResource(obj, resourceCreationDataEx))
          {
            ITxObject tcResource = TxTcUtilities.CreateTcResource(obj, resourceCreationDataEx);
            if (tcResource != null)
            {
              if (tcResource is ITxComponent itxComponent)
                itxComponent.SetModelingScope();
              this.SelectObject(tcResource);
              childUnderTcObject = true;
              break;
            }
            break;
          }
          break;
        }
      case ITxComponent _:
        if (!(obj as ITxComponent).IsEquipment)
          break;
        goto label_2;
    }
    return childUnderTcObject;
  }

  private ResourceManager GetResourceManager()
  {
    return new ResourceManager("DnProcessSimulateCommands.StringTable", this.GetType().Assembly);
  }

  private void SelectObject(ITxObject obj)
  {
    TxObjectList txObjectList = new TxObjectList();
    ((Collection<ITxObject>) txObjectList).Add(obj);
    TxApplication.ActiveSelection.SetItems(txObjectList);
  }
}
